package cn.zyq.ykb.service.course.impl;

import cn.zyq.ykb.mapper.course.CourseMapper;
import cn.zyq.ykb.service.course.CourseService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.beans.course.Course;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 商学院课程Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-19
 */
@Service
@Slf4j
public class CourseServiceImpl  extends BaseService implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询商学院课程
     *
     * @param courseID 商学院课程主键
     * @param number
     * @return 商学院课程
     */
    @Override
    public Course selectCourseByCourseID(Integer courseID, Integer number) {
        Course course = courseMapper.selectByPrimaryKey(courseID);
        course.setNumber(number);
        this.courseMapper.updateByPrimaryKeySelective(course);
        if (course.getDeleted() == 1) {
            return null;
        }
        return course;
    }

    @Override
    public Course selectCourseByCourseID(Integer courseID) {
        Course course = courseMapper.selectByPrimaryKey(courseID);
        int number = course.getNumber();
        ++number;
        log.info("数量：{}",number);
        course.setNumber(number);
        this.courseMapper.updateByPrimaryKeySelective(course);
        if (course.getDeleted() == 1) {
            return null;
        }
        return course;
    }

    /**
     * 查询商学院课程
     *
     * @param serviceID 业务ID
     * @return 商学院课程
     */
    @Override
    public Course selectCourseByServiceID(String serviceID) {
        Course param = new Course();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.courseMapper.selectOne(param);
    }

    /**
     * 查询商学院课程列表
     *
     * @param pageParam
     * @return 商学院课程
     */
    @Override
    public PageInfo<Course> selectCourseList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Course> list = this.courseMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询商学院课程列表
     *
     * @param param
     * @return 商学院课程
     */
    @Override
    public List<Course> selectCourseList(Course param) {
        param.setDeleted(0);
        return this.courseMapper.select(param);
    }
    /**
     * 新增商学院课程
     *
     * @param param 商学院课程
     * @return 结果
     */
    @Override
    public int insertCourse(Course param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.courseMapper.insertSelective(param);
    }

    /**
     * 修改商学院课程
     *
     * @param param 商学院课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.courseMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除商学院课程
     *
     * @param courseIDs 需要删除的商学院课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseIDs(Integer[] courseIDs) {
        int i = 0;
        for (Integer courseID : courseIDs) {
            i += this.deleteCourseByCourseID(courseID);
        }
        return i;
    }

    /**
     * 删除商学院课程信息
     *
     * @param courseID 商学院课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseID(Integer courseID) {
        Course course = this.selectCourseByCourseID(courseID);
        if (course == null) {
            return 0;
        } else {
            Course storeInfo = new Course();
            storeInfo.setCourseID(courseID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.courseMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
